package com.example.iorder.restaurant;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.example.domain.Menu;
import com.example.domain.MenuItem;
import com.example.domain.OrderedMenuItem;
import com.example.iorder.R;

public class ShowMenuAdapter extends BaseExpandableListAdapter {
	private Context context;
	private ArrayList<Menu> groups;
    private ArrayList<ArrayList<MenuItem>> children;
    
    public ShowMenuAdapter(Context context, ArrayList<Menu> groups, ArrayList<ArrayList<MenuItem>> children) {
    	this.context = context;
    	this.groups = groups;
    	this.children = children;
    }
	
	public void addMenuItem(MenuItem itemToAdd) {
		// Add only if there are menus
		if (groups.size() > 0) {
			// Get the menu that the item belongs to
			Menu menuToAddTo = null;
			for (Menu menu : groups) {
				if (menu.getId() == itemToAdd.getMenuId()) {
					menuToAddTo = menu;
					break;
				}
			}
			if (menuToAddTo != null) {
				int menuIndex = groups.indexOf(menuToAddTo);
				ArrayList<MenuItem> listToAddTo = children.get(menuIndex);
				listToAddTo.add(itemToAdd);
			}
		}
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return children.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		ArrayList<MenuItem> childList = getChildrenAt(groupPosition);
		int childCount = childList.size();
		return childCount;
	}
	
	public ArrayList<MenuItem> getChildrenAt(int groupPosition) {
		ArrayList<MenuItem> childList = children.get(groupPosition);
		return childList;
	}
	
	public ArrayList<MenuItem> getChildrenAt(Menu menuOfItem) {
		int menuId = menuOfItem.getId();
		ArrayList<MenuItem> result = null;
		for (ArrayList<MenuItem> childList : children) {
			if (childList.size() > 0) {
				if (childList.get(0).getMenuId() == menuId) {
					result = childList;
					break;
				}
			}
		}
		return result;
	}
	
	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		MenuItem menuItem = (MenuItem) getChild(groupPosition, childPosition);
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.menu_list_item, parent, false);
		}
		// Get the views 
		TextView nameView = (TextView) convertView.findViewById(R.id.menu_item_name);
		TextView priceView = (TextView) convertView.findViewById(R.id.menu_item_price);
		TextView amountView = (TextView) convertView.findViewById(R.id.menu_item_amount);
		
		// Set the values to the views
		if (menuItem != null) {
			// Clip the text if the title is too long
			String formattedTitle = menuItem.getTitle();
			if (menuItem.getTitle().length() > 15) {
				formattedTitle = formattedTitle.substring(0, 13) + "..";
			}
			double price = menuItem.getPrice();
			String formattedPrice = context.getResources().getString(R.string.euro_sign) + " " + Double.toString(menuItem.getPrice());
			// If the price has a decimal
			if (price % 1 != 0) {
				// Check if price has one decimal after the comma, e.g. 25,5 and make it 25,50
				double multipliedPrice = price * 10;
				if (multipliedPrice % 1 == 0) {
					formattedPrice += "0";
				}
			}
			//String formattedAmount = Integer.toString(orderedMenuItem.getCurrentAmount());
			
			nameView.setText(formattedTitle);
			priceView.setText(formattedPrice);
			//amountView.setText(formattedAmount);
			
			// Give the view the identifiers of the menu and menu item
			convertView.setTag(R.id.menuID, menuItem.getMenuId());
			convertView.setTag(R.id.menuItemID, menuItem.getId());
		}

		return convertView;
	}
	
	public void addMenu(Menu itemToAdd) {
		groups.add(itemToAdd);
		children.add(new ArrayList<MenuItem>());
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		if (groups != null)
			return groups.size();
		return 0;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		Menu menu = (Menu) getGroup(groupPosition);
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.menu_list_group, parent, false);
		}
		// Get the views
		TextView menuName = (TextView) convertView.findViewById(R.id.lblListHeader);
		
		// Set the values to the views
		if (menu != null) {
			menuName.setText(menu.getTitle());
			menuName.setTag(menu.getId());
		}
		
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	
	@Override
    public boolean areAllItemsEnabled() {
        return true;
    }
	
	public ArrayList<ArrayList<MenuItem>> getAllChildren() {
		return children;
	}
	
	public void clearChildrenData() {
		for (ArrayList<MenuItem> childList : children) {
			childList.clear();
		}
	}

}
